package cn.tedu.train.management.dao.persist.repository.impl;

import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.util.PageInfoToPageDataConverter;
import cn.tedu.train.management.dao.persist.repository.ITrainRepository;
import cn.tedu.train.management.mapper.TrainMapper;
import cn.tedu.train.management.pojo.entity.Train;
import cn.tedu.train.management.pojo.vo.TrainListItemVO;
import cn.tedu.train.management.pojo.vo.TrainNameListVO;
import cn.tedu.train.management.pojo.vo.TrainStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 说明：
 *
 * @author：ChenJJ
 * @date:2023/6/25
 **/
@Slf4j
@Repository
public class TrainRepositoryImpl implements ITrainRepository {
    @Autowired
    private TrainMapper trainMapper;

    public TrainRepositoryImpl() {
        log.debug("创建数据访问实现类对象：TrainRepositoryImpl");
    }
    @Override
    public int insert(Train train) {
        log.debug("开始执行【插入列车信息】的数据访问，参数：{}", train);
        return trainMapper.insert(train);
    }

    @Override
    public int insertBatch(List<Train> trainList) {
        log.debug("开始执行【批量列车信息】的数据访问，参数：{}", trainList);
        return trainMapper.insertBatch(trainList);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除列车信息】的数据访问，参数：{}", id);
        return trainMapper.deleteById(id);
    }

    @Override
    public int update(Train train) {
        log.debug("开始执行【修改列车信息】的数据访问，参数：{}", train);
        return trainMapper.updateById(train);
    }

    @Override
    public TrainStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询列车信息】的数据访问，参数：{}", id);
        return trainMapper.getStandardById(id);
    }


    @Override
    public int countByName(String name) {
        log.debug("开始执行【统计匹配名称的列车信息的数量】的数据访问，参数：{}", name);
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return trainMapper.selectCount(queryWrapper);
    }

    @Override
    public List<TrainNameListVO> listTrainName() {
        return trainMapper.listTrainName();
    }


    @Override
    public int countByDepartmentId(Long number) {
        log.debug("开始执行【统计匹配列车信息的数量】的数据访问，相册：{}", number);
        QueryWrapper<Train> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", number);
        return trainMapper.selectCount(queryWrapper);
    }

    @Override
    public PageData<TrainListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询列车信息列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<TrainListItemVO> list = trainMapper.list();
        PageInfo<TrainListItemVO> pageInfo = new PageInfo<>(list);
        PageData<TrainListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

}
